package com.starlight.flower_server.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starlight.flower_server.controller.authentication.AuthenticationCheck;
import com.starlight.flower_server.controller.exception.BadRequestException;
import com.starlight.flower_server.domain.TotalData;
import com.starlight.flower_server.domain.comment.cqrs.CommentPageQuery;
import com.starlight.flower_server.domain.comment.data.CommentData;
import com.starlight.flower_server.infrastructure.FlowerServerResponse;
import com.starlight.flower_server.infrastructure.RoleType;
import com.starlight.flower_server.infrastructure.Status;
import com.starlight.flower_server.infrastructure.entity.Comment;
import com.starlight.flower_server.domain.service.CommentService;
import com.starlight.flower_server.infrastructure.util.CurrentUserInfoUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author yfy2001
 * @date 2023/4/6 11:30
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private CommentService commentService;

    @GetMapping("/getCommentDetailById")
    public FlowerServerResponse<Comment> getCommentDetailById(Long id){
        return FlowerServerResponse.success(commentService.queryCommentDetailById(id));
    }

    @PostMapping("/deleteCommentById")
    @AuthenticationCheck(roleType = RoleType.ROOT)
    public FlowerServerResponse<Integer> deleteCommentById(Long id){
        return FlowerServerResponse.success(commentService.logicDeleteCommentById(id));
    }

    @PostMapping("/createComment")
    public FlowerServerResponse<Integer> createComment(@RequestBody Comment comment){
        return FlowerServerResponse.success(commentService.insertComment(comment));
    }

    @PostMapping("/updateComment")
    public FlowerServerResponse<Integer> updateComment(@RequestBody Comment comment){
        return FlowerServerResponse.success(commentService.modifyComment(comment));
    }

    /**
     * 用户发表评论
     * @param comment
     * @return
     */
    @PostMapping("/publishComment")
    @AuthenticationCheck(roleType = {RoleType.ROOT,RoleType.NORMAL,RoleType.PRIMARY,RoleType.SENIOR})
    public FlowerServerResponse<Integer> publishComment(Comment comment){
        Long userId= Long.valueOf(CurrentUserInfoUtils.getUserId());
        comment.setFlowerUserId(userId);
        if (comment.getArticleId()==null){
            throw new BadRequestException(Status.INVALID_ARTICLE_ID.getMessage(),Status.INVALID_ARTICLE_ID);
        }
        return FlowerServerResponse.success(commentService.insertComment(comment));
    }

    @GetMapping("/getCommentPage")
    public FlowerServerResponse<IPage<CommentData>> getCommentPage(CommentPageQuery query){
        return FlowerServerResponse.success(commentService.queryCommentDataList(query));
    }

    @GetMapping("/getCommentListByArticleId")
    public FlowerServerResponse<List<CommentData>> getCommentList(Long articleId){
        return FlowerServerResponse.success(commentService.queryCommentDataListByArticleId(articleId));
    }

    @GetMapping("/getAllCommentData")
    public FlowerServerResponse<TotalData<CommentData>> getAllCommentData(){
        return FlowerServerResponse.success(commentService.queryAllCommentData());
    }

    @GetMapping("/getCommentTotalCount")
    public FlowerServerResponse<TotalData<CommentData>> getCommentTotalCount(){
        return FlowerServerResponse.success(commentService.queryCommentTotalCount());
    }
}
